package com.taomeng.service.impl;

import com.taomeng.dao.PermissionDao;
import com.taomeng.entity.PerDTree;
import com.taomeng.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional
@Service
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    private PermissionDao permissionDao;


    @Override
    public List<PerDTree> findPermissionByRoleId(String roleId) {
        List<PerDTree> allPermissions = permissionDao.findAllPermission();
        List<PerDTree> rolePermissions = permissionDao.findPermissionByRoleId(roleId);

        //将已有的权限选中
        for (PerDTree perDTree : allPermissions) {
            for (PerDTree rolePermission : rolePermissions) {
                if (rolePermission.getId().equals(perDTree.getId())){
                    perDTree.setCheckArr(rolePermission.getCheckArr());
                }
            }
        }
        return allPermissions;

    }

    @Override
    public List<PerDTree> findAllPermission() {
        List<PerDTree> permissions = permissionDao.findAllPermission();
        return permissions;
    }


}
